package co.rcantor.dev.siasar.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import co.rcantor.dev.siasar.R;

public class LoginControl extends LinearLayout{
	
	EditText txtUsuario;
	EditText txtPassword;
	String pais;
	Button btnLogin;
	TextView lblMensaje;
	LoginListener listener;
	
    public LoginControl(Context context) {
       super(context);
       inicializar();
    }
    
    public LoginControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        inicializar();
    }
    
    private void inicializar(){
    	
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
        li.inflate(R.layout.login_ctrl, this, true);
        
        final String[] datos = new String[]{"PANAMA","NICARAGUA","HONDURAS"};
         
        ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_item, datos);
     
        txtUsuario = (EditText)findViewById(R.id.TxtUsuario);        
        txtPassword = (EditText)findViewById(R.id.TxtPassword);
        txtPassword.setText("CLAVE");
        txtUsuario.setText("USUARIO");
        txtUsuario.requestFocus();
        Spinner cmbOpciones = (Spinner)findViewById(R.id.SelPais);        
        adaptador.setDropDownViewResource(android.R.layout.simple_spinner_item);         
        cmbOpciones.setAdapter(adaptador);
        cmbOpciones.setOnItemSelectedListener(new ComboListener());
        btnLogin = (Button)findViewById(R.id.BtnAceptar);
        lblMensaje = (TextView)findViewById(R.id.LblMensaje);
     
        asignarEventos();
    }
    
    private void asignarEventos()
    {
        btnLogin.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                listener.onLogin(txtUsuario.getText().toString(),txtPassword.getText().toString(),getPais());
            }
        });
    }
    
    public class ComboListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	setPais((pos+1)+"");
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	setPais("0");
	    }
	}
    
    public void setMensaje(String msg){
        lblMensaje.setText(msg);
    }
    
    public void setLoginListener(LoginListener l){
    	listener = l;
    }
    
    public String getPais() {
		return pais;
	}

	public void setPais(String pais) {
		this.pais = pais;
	}
}